<template>
  <div style="font-size: 12px;display: flex">
    <div style="flex:1">
      <span :class="collapseBtnClass" style="cursor: pointer;font-size: 18px" @click="collapse"></span>
      <el-breadcrumb separator="/" style="display: inline-block;margin-left: 20px">
        <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
        <el-breadcrumb-item
            :to="{ path: item.path }"
            v-for="item in paths"
            :key="item.title"
        >{{item.title}}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <el-dropdown style="width: 150px;cursor: pointer;text-align: right">
      <div style="display: inline-block">
        <img :src="user.avatarUrl" style="width: 30px;border-radius: 50%;position: relative;top:10px;right: 5px">
        <span>{{user.nickname}}<i class="el-icon-arrow-down" style="margin-left: 5px"></i></span>
      </div>
      <el-dropdown-menu slot="dropdown">
        <router-link to="/managerInfo" style="text-decoration: none;font-size: 14px">个人信息</router-link>
        <el-dropdown-item>
          <span  @click="logout">退出</span>
        </el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
  </div>
</template>

<script>
export default {
  name: "Header",
  props: {
    collapseBtnClass: String,
    collapse: Function,
    user: Object
  },
  data(){
    return{
      paths:[],
    }
  },
  methods:{
    caleBreadcrumb() {
      const temp = []
      this.$route.matched.forEach((item) => {
        if (item.meta.title && item.path) {
          temp.push({ path: item.path, title: item.meta.title })
        }
      })
      this.paths = temp
    },
    logout(){
      this.$router.push("/")
      localStorage.removeItem("user")
      this.$message.success("退出成功！")
    }
  },
  created() {
    this.caleBreadcrumb() //初始化渲染面包屑导航
  },
  watch: {
    '$route.path': {
      handler() {
        this.caleBreadcrumb()
      },
      immediate: true
    }
  }
}
</script>

<style scoped>

</style>